package com.foogui.primary.sync;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class SyncTicket implements Ticket{
    private int count = 20;


    public synchronized void removeTicket() throws InterruptedException {
        //这里必须使用while判断，否则可能出现虚假唤醒的问题
        while (count <= 0) this.wait();
        System.out.println(Thread.currentThread().getName() + "卖了一张票，还剩" + (--count) + "张");
        Thread.sleep(100);
        this.notifyAll();
    }


    public synchronized void addTicket() throws InterruptedException {
        //这里必须使用while判断，否则可能出现虚假唤醒的问题
        while (count != 0) this.wait();
        count = 20;
        System.out.println("重新补充票数，总票数为" + count + "张");
        Thread.sleep(100);
        this.notifyAll();
    }
}

